<?php
/**
 * Plugin Name: WooCommerce Hooks for Woo
 * Plugin URI: http://www.facebook.com/NguyenDuyPhong
 * Description: Hooks for Woo inside Settings page.
 * Author: PhongND
 * Author URI: http://www.facebook.com/NguyenDuyPhong
 * Version: 1.0
 * Text Domain: wc-hooks_for_woo-aaa01
 * Domain Path: /languages/
 *
 * Copyright: (c) 2014 phongnd
 *
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * @package   wc-hooks_for_woo-aaa02
 * @author    PhongND
 * @category  Utility
 * @copyright Copyright (c) 2014, PhongND.
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
 */

// if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

// // Check if WooCommerce is active
// if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) )
	// return;

// // WC version check
// if ( version_compare( get_option( 'woocommerce_db_version' ), '2.1', '<' ) ) {

	// function woocommerce_customizer_outdated_version_notice() {

		// $message = sprintf(
			// __( '%sWooCommerce Customizer is inactive.%s This version requires WooCommerce 2.1 or newer. Please %supdate WooCommerce to version 2.1 or newer%s', 'wc_hooks_for_woo_domain' ),
			// '<strong>',
			// '</strong>',
			// '<a href="' . admin_url( 'plugins.php' ) . '">',
			// '&nbsp;&raquo;</a>'
		// );

		// echo sprintf( '<div class="error"><p>%s</p></div>', $message );
	// }

	// add_action( 'admin_notices', 'woocommerce_customizer_outdated_version_notice' );

	// return;
// }


/**
 * # WooCommerce Customizer Main Plugin Class
 *
 * ## Plugin Overview
 *
 * Adds a few settings pages which make uses of some of the simpler filters inside WooCommerce, so if you want to quickly
 * change button text or the number of products per page, you can use this instead of having to write code for the filter.
 * Note this isn't designed as a rapid development/prototyping tool -- for a production site you should use the actual filter
 * instead of relying on this plugin.
 *
 * ## Admin Considerations
 *
 * A 'Customizer' sub-menu page is added to the top-level WooCommerce page, which contains 4 tabs with the settings
 * for each section - Shop Loop, Product Page, Checkout, Misc
 *
 * ## Frontend Considerations
 *
 * The filters that the plugin exposes as settings as used exclusively on the frontend.
 *
 * ## Database
 *
 * ### Global Settings
 *
 * + `wc_customizer_active_customizations` - a serialized array of active customizations in the format
 * filter name => filter value
 *
 * ### Options table
 *
 * + `wc_customizer_version` - the current plugin version, set on install/upgrade
 *
 */
 

class wc_hooks_for_woo {


	/** plugin version number */
	const VERSION = '1.0.1';

	/** @var \wc_hooks_for_woo instance */
	public $settings;

	/** var array the active filters */
	public $filters;


	/**
	 * Initializes the plugin
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		
		
		// load translation
		add_action( 'init', array( $this, 'load_translation' ) );

		// admin
		if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {

			// load settings page
			add_filter( 'woocommerce_get_settings_pages', array( $this, 'add_settings_page' ) );

			// add a 'Configure' link to the plugin action links
			add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_plugin_action_links' ) );

			// run every time
			$this->install();
		}
		
		add_action( 'woocommerce_init', array( $this, 'load_customizations' ) );
	}


	/**
	 * Add settings page
	 *
	 * @since 2.0.0
	 * @param array $settings
	 * @return array
	 */
	public function add_settings_page( $settings ) {

		$settings[] = require_once( 'includes/class-wc-hooks_for_woo-settings.php' );
		return $settings;
	}


	/**
	 * Load customizations after WC is loaded so the version can be checked
	 *
	 * @since 1.2.0
	 */
	public function hwif_notice($strName) {
		$message = sprintf(
			__( '%s%swc-hooks_for_woo%s is old now. Please update WooCommerce. Description: %s', 'wc_hooks_for_woo_domain' ),
			'<br/>',
			'<strong>',
			'</strong>',
			 $strName
		);
		// $strName
		echo $message;
	}
	public function load_customizations() {

		// nono, load filter names and values
		// $this->filters = get_option( 'wc_customizer_active_customizations' ); // nono
		$chk01 = get_option('hwif_chk01');
		$desc = "My Desc...";
		$desc = !empty($desc) ? $desc : '_ empty _' ;
		if ($chk01 == 'yes')
		{
			//admin.less
			// for front-end.
			add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
			
			add_action( 'admin_notices', call_user_func_array(array( $this, 'hwif_notice'), array($desc))); // PhongND 2014/11/12 
			
			 //my customizations.
			// Hook in
			add_action( 'woocommerce_after_order_notes', array($this, 'hfw_add_icon_to_checkout' ));
			
			//actions for cart
			add_action( 'woocommerce_before_cart_totals', array($this, 'hfw_before_cart_totals' ));
			add_action( 'woocommerce_before_cart_contents', array($this, 'hfw_before_cart_contents' ));
			add_action( 'woocommerce_before_cart_table', array($this, 'hfw_before_cart_table' ));
			add_action( 'woocommerce_cart_collaterals', array($this, 'hfw_cart_collaterals' ));
			add_action( 'woocommerce_cart_contents', array($this, 'hfw_cart_contents' ));
			add_action( 'woocommerce_cart_coupon', array($this, 'hfw_cart_coupon' ));
			add_action( 'woocommerce_cart_has_errors', array($this, 'hfw_cart_has_errors' )); // nono 
			add_action( 'woocommerce_cart_is_empty', array($this, 'hfw_cart_is_empty' )); // ok
			add_action( 'woocommerce_cart_totals_after_order_total', array($this, 'hfw_cart_totals_after_order_total' )); // ok
			add_action( 'woocommerce_cart_totals_after_shipping', array($this, 'hfw_cart_totals_after_shipping' )); // ok
			add_action( 'woocommerce_cart_totals_before_order_total', array($this, 'hfw_cart_totals_before_order_total' )); // nono, before/after TOTAL??
			add_action( 'woocommerce_review_order_after_cart_contents', array($this, 'hfw_review_order_after_cart_contents' )); // nono
			add_action( 'woocommerce_review_order_before_cart_contents', array($this, 'hfw_review_order_before_cart_contents' )); // nono
			add_action( 'woocommerce_widget_shopping_cart_before_buttons', array($this, 'hfw_widget_shopping_cart_before_buttons' )); // nono 
			
			//actions for checkout
			add_action( 'woocommerce_after_checkout_billing_form', array($this, 'hfw_after_checkout_billing_form' ));
			add_action( 'woocommerce_after_checkout_form', array($this, 'hfw_after_checkout_form' ));
			add_action( 'woocommerce_after_checkout_registration_form', array($this, 'hfw_after_checkout_registration_form' )); //nono
			add_action( 'woocommerce_after_checkout_shipping_form', array($this, 'hfw_after_checkout_shipping_form' )); //ok, but check again below item "Order Notes"
			add_action( 'woocommerce_before_checkout_billing_form', array($this, 'hfw_before_checkout_billing_form' )); //
			add_action( 'woocommerce_before_checkout_form', array($this, 'hfw_before_checkout_form' )); //
			add_action( 'woocommerce_before_checkout_shipping_form', array($this, 'hfw_before_checkout_shipping_form' )); //
			add_action( 'woocommerce_before_checkout_registration_form', array($this, 'hfw_before_checkout_registration_form' )); //nono
			add_action( 'woocommerce_checkout_after_customer_details', array($this, 'hfw_checkout_after_customer_details' )); //
			add_action( 'woocommerce_checkout_before_customer_details', array($this, 'hfw_checkout_before_customer_details' )); //
			add_action( 'woocommerce_checkout_billing', array($this, 'hfw_checkout_billing' )); //
			add_action( 'woocommerce_checkout_order_review', array($this, 'hfw_checkout_order_review' )); //
			
			
			//filters for ...
			$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart ); // nono 
			
			
			
			// add_filter( 'woocommerce_checkout_fields' , array($this, 'custom_override_checkout_fields' ));
			// add_action( 'woocommerce_after_order_notes', array($this, 'my_custom_checkout_field' ));
			// // nono, add_action('woocommerce_checkout_process', array($this, 'my_custom_checkout_field_process')); 
	 		// add_action( 'woocommerce_admin_order_data_after_billing_address', array($this, 'my_custom_checkout_field_display_admin_order_meta'), 10, 1 );
		}
		else
			add_action( 'admin_notices', call_user_func_array(array( $this, 'hwif_notice'), array("Nothing..."))); // PhongND 2014/11/12 
		
		
		
		// // only add filters if some exist
		// if ( ! empty( $this->filters ) ) {

			// foreach ( $this->filters as $filter_name => $filter_value ) {

				// // WC 2.1 changed the add to cart text filter signatures so conditionally add the new filters
				// if ( false !== strpos( $filter_name, 'add_to_cart_text' ) ) {

					// if ( $filter_name == 'single_add_to_cart_text' ) {

						// add_filter( 'woocommerce_product_single_add_to_cart_text', array( $this, 'customize_single_add_to_cart_text' ) );

					// } else {

						// add_filter( 'woocommerce_product_add_to_cart_text', array( $this, 'customize_add_to_cart_text' ), 10, 2 );
					// }

				// } else {

					// add_filter( $filter_name, array( $this, 'customize' ) );
				// }
			// }
		// }
	}
	
	
	/**
	 * Add Hooks for Woo on Checkout page.
	 */
	function hfw_add_icon_to_checkout( $checkout ) {

		echo '<div id="div_wc_hooks_for_woo"><h2>' . __('Hooks for Woo (NOT showing???)', 'wc_hooks_for_woo_domain') . '</h2>';
		echo '<a href="#" class="hfw_phongnd_class">An icon</a>';
		
		// nono, using woocommerce fonts??
		echo '<span class="wcicon-virtual"></span><span class="mls">wcicon-virtual - </span>';
		echo '</div>';

	}
	
	
	/**
	 * Add woocommerce_before_cart_totals
	 */
	function hfw_before_cart_totals( $checkout ) {
	
		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_before_cart_totals...</a>';	
		echo '</div>';

	}
	
	/**
	 * Add woocommerce_before_cart_contents
	 */
	function hfw_before_cart_contents( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_before_cart_contents...</a>';
		
		echo '</div>';

	}
	
	/**
	 * Add woocommerce_before_cart_table
	 */
	function hfw_before_cart_table( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_before_cart_table...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_cart_collaterals
	 */
	function hfw_cart_collaterals( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_cart_collaterals...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_cart_contents
	 */
	function hfw_cart_contents( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_cart_contents...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_cart_coupon
	 */
	function hfw_cart_coupon( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_cart_coupon...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_cart_has_errors
	 */
	function hfw_cart_has_errors( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_cart_has_errors...</a>';
		
		echo '</div>';

	}
	
	/**
	 * Add woocommerce_cart_is_empty
	 */
	function hfw_cart_is_empty( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_cart_is_empty...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_cart_totals_after_order_total
	 */
	function hfw_cart_totals_after_order_total( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_cart_totals_after_order_total...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_cart_totals_after_shipping
	 */
	function hfw_cart_totals_after_shipping( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_cart_totals_after_shipping...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_cart_totals_before_order_total
	 */
	function hfw_cart_totals_before_order_total( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_cart_totals_before_order_total...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_review_order_after_cart_contents
	 */
	function hfw_review_order_after_cart_contents( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_review_order_after_cart_contents...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_review_order_before_cart_contents
	 */
	function hfw_review_order_before_cart_contents( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_review_order_before_cart_contents...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_widget_shopping_cart_before_buttons
	 */
	function hfw_widget_shopping_cart_before_buttons( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_widget_shopping_cart_before_buttons...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_add_to_cart_handler
	 */
	function hfw_add_to_cart_handler( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_add_to_cart_handler...</a>';
		
		echo '</div>';

	}
	
	/**
	 * Add woocommerce_after_checkout_billing_form
	 */
	function hfw_after_checkout_billing_form( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_after_checkout_billing_form...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_after_checkout_form
	 */
	function hfw_after_checkout_form( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_after_checkout_form...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_after_checkout_registration_form
	 */
	function hfw_after_checkout_registration_form( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_after_checkout_registration_form...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_after_checkout_shipping_form
	 */
	function hfw_after_checkout_shipping_form( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_after_checkout_shipping_form...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_before_checkout_billing_form
	 */
	function hfw_before_checkout_billing_form( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_before_checkout_billing_form...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_before_checkout_form
	 */
	function hfw_before_checkout_form( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_before_checkout_form...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_before_checkout_shipping_form
	 */
	function hfw_before_checkout_shipping_form( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_before_checkout_shipping_form...</a>';
		
		echo '</div>';

	}
	
	/**
	 * Add woocommerce_before_checkout_registration_form
	 */
	function hfw_before_checkout_registration_form( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_before_checkout_registration_form...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_checkout_after_customer_details
	 */
	function hfw_checkout_after_customer_details( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_checkout_after_customer_details...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_checkout_before_customer_details
	 */
	function hfw_checkout_before_customer_details( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_checkout_before_customer_details...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_checkout_billing
	 */
	function hfw_checkout_billing( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_checkout_billing...</a>';
		
		echo '</div>';

	}
	/**
	 * Add woocommerce_checkout_order_review
	 */
	function hfw_checkout_order_review( $checkout ) {

		echo '<div id="" class="">';
		echo '<a href="#" class="hfw_phongnd_class">woocommerce_checkout_order_review...</a>';
		
		echo '</div>';

	}
	
	
	
	
	
	
	
	 /**
	 * Process the checkout
	 */
	function my_custom_checkout_field_process() {
		// Check if set, if its not set add an error.
		// if ( ! $_POST['my_field_name'] )
			wc_add_notice( __( 'Please enter something into this new shiny field.', 'wc_hooks_for_woo_domain'), 'error' );
	}
			
	/**
	 * Handle localization, WPML compatible
	 *
	 * @since 1.1.0
	 */
	public function load_translation() {

		// localization in the init action for WPML support
		load_plugin_textdomain( 'wc_hooks_for_woo_domain', false, dirname( plugin_basename( __FILE__ ) ) . '/i18n/languages' );
	}


	/** Frontend methods ******************************************************/


	/**
	 * Add hook to selected filters
	 *
	 * @since 1.0.0
	 * @return string $filter_value value to use for selected hook
	 */
	public function customize() {

		$current_filter = current_filter();

		if ( isset( $this->filters[ $current_filter ] ) ) {

			if ( 'customizer_true' === $this->filters[ $current_filter] || 'customizer_true' === $this->filters[ $current_filter] ) {

				// helper to return a pure boolean value
				return 'customizer_true' === $this->filters[ $current_filter ];

			} else {

				return $this->filters[ $current_filter ];
			}
		}

		// no need to return a value passed in, because if a filter is set, it's designed to only return that value
	}


	/** Admin methods ******************************************************/


	/**
	 * Return the plugin action links.  This will only be called if the plugin
	 * is active.
	 *
	 * @since 1.0.0
	 * @param array $actions associative array of action names to anchor tags
	 * @return array associative array of plugin action links
	 */
	public function add_plugin_action_links( $actions ) {

		$custom_actions = array(
			'configure' => sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=wc-settings&tab=wc_hooks_for_woo_id&section=' ), __( 'Configure', 'wc_hooks_for_woo_domain' ) ),
			'faq'       => sprintf( '<a href="%s">%s</a>', 'http://facebook.com/NguyenDuyPhong/', __( 'FAQ', 'wc_hooks_for_woo_domain' ) ),
			'support'   => sprintf( '<a href="%s">%s</a>', 'http://facebook.com/NguyenDuyPhong/', __( 'Support', 'wc_hooks_for_woo_domain' ) ),
		);

		// add the links to the front of the actions list
		return array_merge( $custom_actions, $actions );
	}


	/** Lifecycle methods ******************************************************/


	/**
	 * Run every time.  Used since the activation hook is not executed when updating a plugin
	 *
	 * @since 1.1.0
	 */
	private function install() {

		// get current version to check for upgrade
		$installed_version = get_option( 'wc_customizer_version' );

		// install
		if ( ! $installed_version ) {

			// install default settings
		}

		// upgrade if installed version lower than plugin version
		if ( -1 === version_compare( $installed_version, self::VERSION ) ) {
			$this->upgrade( $installed_version );
		}
	}


	/**
	 * Perform any version-related changes.
	 *
	 * @since 1.1.0
	 * @param int $installed_version the currently installed version of the plugin
	 */
	private function upgrade( $installed_version ) {

		// update the installed version option
		update_option( 'wc_customizer_version', self::VERSION );
	}

	/**
	 * Add scripts
	 */
	public function scripts() {
		global $woocommerce;

		wp_enqueue_style( 'wc-hooks_for_woo', plugins_url( 'assets/css/style.css', __FILE__ ) );
		
		// wp_enqueue_script( 'wc-hooks_for_woo', plugins_url( 'assets/js/wc-hooks_for_woo.js', __FILE__ ), array( 'wc-hooks_for_woo_scripts' ), '1.0', true ); // nono , ???
		// wp_enqueue_script( 'wc-hooks_for_woo-script', plugins_url( '/assets/js/wc-hooks_for_woo.js', __FILE__ ) ); // ok
	}

}


/**
 * The wc_hooks_for_woo global object
 * @name $wc_hooks_for_woo
 * @global wc_hooks_for_woo $GLOBALS['wc_hooks_for_woo']
 */
$GLOBALS['wc_hooks_for_woo'] = new wc_hooks_for_woo();
